/*
 * 用户界面验证js
 * 依赖组件[bootstrapValidator]
 */
(function() {
	require(['jquery', 'vbox', 'select2'], function(jquery, vbox) {
		
		if(typeof jQuery === 'undefined') {	//检测jquery依赖项
			throw new Error('jquery依赖项未能加载');
		}
		
		if(typeof $.fn.bootstrapValidator === 'undefined') {	//检测bootstrapValidator依赖项
			throw new Error('bootstrapValidator依赖项未能加载');
		}
		/*字典添加页面所属应用下拉列表*/
		var $eventSelect1 =null , 
			formData = [{id:"男", text:"男"}, {id:"女", text:"女"}];
		
		$eventSelect1 = $("#update_gender").select2({
			 placeholder: "性别",
			 allowClear: true,
			 data: formData,
			 minimumResultsForSearch: Infinity
		 });
		$eventSelect1.val($('#person_update_form').data('row').gender).trigger("change");
		
		$.ajax({
			url: "role/retrieveUserRoles.service",
			type: "get",
			success: function(data) {
				if(data && data.success) {
					var _key = data.myKey;
					var _value = data.myValue;
					var select2Data = toJsonData(_key, _value);
					$eventSelect2 = $("#update_rolename").select2({
						 placeholder: "角色",
						 allowClear: true,
						 data: select2Data,
						 multiple: true,
						 minimumResultsForSearch: Infinity
					 }).val($('#person_update_form').data('row').rids).trigger("change");
    			} else if(data && data.success == false) {
    				alert(data.errorMsg);
    			} else {
    				alert("error");
    			}
			}
		});
		
		function toJsonData(myKey, myValue) {
			var arr = [], childrenArr = [], _childrenArr=[], childrenArrLen = 0, childrenObj={}, childObj = {},
				_text = myKey, textLen = _text.length, text = "",
				_children = myValue,
				i = 0, j =0;
				
				for(; i<textLen; i++) {
					text = _text[i];
					childrenArr = _children[text];
					_childrenArr = [];
					for(j = 0,childrenArrLen = childrenArr.length; j<childrenArrLen; j++) {
						childrenObj = childrenArr[j];
						childObj = new Object();
						childObj.id = childrenObj.id;
						childObj.text = childrenObj.name;
						_childrenArr.push(childObj);
					}
					arr.push({text: text, children: _childrenArr});
				}
			return arr;
		}
		
		/*验证添加*/
		var v = vbox;
		$('#person_update_form').bootstrapValidator({
	        feedbackIcons:v.feedbackIcons,
	        fields: {
	        	username: {
	        		trigger: 'blur',
	                validators: {
	                    notEmpty: v.notEmpty,
	                    stringLength: v.stringLength100,
	                    regexp: v.regexpCode,
	                    callback: {
	                    	message: '用户名重复',
	                        callback: function(value, validator) {
	                        	var flag = false;
	                        	var id = $('#person_update_form').data('row').id;
	                        	$.ajax({
	                				url: 'user/validateUserInfo.service',
	                				type: 'post',
	                				async: false,
	                				data: {
	                					id: id,
	                					username: value
	                					},
	                				success: function(data) {
	                					if(data.errorCode == "1003"){
	                						flag = false;
	                					}else{
	                						flag = true;
	                					}
	                				}
	                			});
	                        	return flag;
	                        }
	                    }
	                }
	            },
	            name:{
	            	trigger: 'blur',
	                validators: {
	                    notEmpty: v.notEmpty,
	                    stringLength: v.stringLength500,
	                    regexp: v.regexpPname
	                }
	            },
	            gender:{
	            	validators: {
	            		notEmpty: v.notEmpty
	            	}
	            },
	            telephone: {
	            	trigger: 'blur',
	                validators: {
	                	notEmpty: v.notEmpty,
	                    regexp: v.regexpPhone,
	                    callback: {
	                    	message: '联系电话重复',
	                        callback: function(value, validator) {
	                        	var flag = false;
	                        	var id = $('#person_update_form').data('row').id;
	                        	$.ajax({
	                				url: 'user/validateUserInfo.service',
	                				type: 'post',
	                				async: false,
	                				data: {
	                					id: id,
	                					telephone: value
	                					},
	                				success: function(data) {
	                					if(data.errorCode == "1003"){
	                						flag = false;
	                					}else{
	                						flag = true;
	                					}
	                				}
	                			});
	                        	return flag;
	                        }
	                    }
	                }
	            },
	            email: {
	            	trigger: 'blur',
	                validators: {
	                	notEmpty: v.notEmpty,
	                    emailAddress: v.emailAddress
	                }
	            }
	        }
	    });
	});
})();